package com.meilingcloud.web.controller.system;

import com.meilingcloud.common.annotation.Log;
import com.meilingcloud.common.constant.UserConstants;
import com.meilingcloud.common.core.controller.BaseController;
import com.meilingcloud.common.core.domain.AjaxResult;
import com.meilingcloud.common.core.page.TableDataInfo;
import com.meilingcloud.common.enums.BusinessType;
import com.meilingcloud.common.utils.SecurityUtils;
import com.meilingcloud.system.domain.SysCustomForm;
import com.meilingcloud.system.service.ISysCustomFormService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 自定义表单 Controller
 *
 * @author chenl
 * @date 2021-01-29
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/form")
public class SysCustomFormController extends BaseController {

    private final ISysCustomFormService iSysCustomFormService;

    /**
     * 查询自定义表单 列表
     */
    @PreAuthorize("@ss.hasPermi('system:form:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysCustomForm sysCustomForm) {
        startPage();
        List<SysCustomForm> list = iSysCustomFormService.queryList(sysCustomForm);
        return getDataTable(list);
    }

    /**
     * 根据条件查询所有
     */
    @PreAuthorize("@ss.hasPermi('system:form:list')")
    @GetMapping("/allList")
    public AjaxResult allList(SysCustomForm sysCustomForm) {
        List<SysCustomForm> list = iSysCustomFormService.queryList(sysCustomForm);
        return AjaxResult.success(list);
    }

    /**
     * 获取自定义表单 详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:form:query')")
    @GetMapping(value = "/{formId}")
    public AjaxResult getInfo(@PathVariable("formId") Long formId) {
        return AjaxResult.success(iSysCustomFormService.getById(formId));
    }

    /**
     * 新增自定义表单
     */
    @PreAuthorize("@ss.hasPermi('system:form:add')")
    @Log(title = "自定义表单 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody SysCustomForm sysCustomForm) {
        if (UserConstants.NOT_UNIQUE.equals(iSysCustomFormService.checkFormKeyUnique(sysCustomForm))) {
            return AjaxResult.error("新增自定义表单" + sysCustomForm.getFormKey() + "失败，表单Key已存在");
        }
        sysCustomForm.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iSysCustomFormService.save(sysCustomForm) ? 1 : 0);
    }

    /**
     * 修改自定义表单
     */
    @PreAuthorize("@ss.hasPermi('system:form:edit')")
    @Log(title = "自定义表单 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCustomForm sysCustomForm) {
        if (UserConstants.NOT_UNIQUE.equals(iSysCustomFormService.checkFormKeyUnique(sysCustomForm))) {
            return AjaxResult.error("修改自定义表单" + sysCustomForm.getFormKey() + "失败，表单Key已存在");
        }
        sysCustomForm.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iSysCustomFormService.updateById(sysCustomForm) ? 1 : 0);
    }

    /**
     * 删除自定义表单
     */
    @PreAuthorize("@ss.hasPermi('system:form:remove')")
    @Log(title = "自定义表单 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{formIds}")
    public AjaxResult remove(@PathVariable Long[] formIds) {
        return toAjax(iSysCustomFormService.removeByIds(Arrays.asList(formIds)) ? 1 : 0);
    }

    /**
     * 渲染表单
     */
    @PostMapping("/formJsonShow")
    public AjaxResult formDataShow(@RequestBody SysCustomForm sysCustomForm) {
        return AjaxResult.success(iSysCustomFormService.formDataShow(sysCustomForm.getFormKey()));
    }
}
